package com.tiles.tools;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.InvalidClaimException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.tiles.entity.TilesUser;

import java.security.SignatureException;
import java.util.Calendar;
import java.util.UUID;

public class JwtUtil {
    //密钥 （salt盐）
    private static final String KEY = "tiles";
    /*
    * 存入用户ID
    * 用户最后登陆时间
    * */
    public static String creatJwtName(String name) {
        // 过期时间
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR,7);

        JWTCreator.Builder builder = JWT.create();
         //       Claim声明
         builder.withClaim("name", name)
                .withClaim("lastLoginTime", System.currentTimeMillis())
                .withExpiresAt(calendar.getTime());
        String token = builder.sign(Algorithm.HMAC256(KEY));
        System.out.println(token);
        return token;
    }
    /*
    * 存入User对象
    * */
    public  static  String creatJwtUser(TilesUser tilesUser){
        // 过期时间
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR,7);

        JWTCreator.Builder builder = JWT.create();
        //       Claim声明
        builder.withClaim("UserInfo", JSONObject.toJSONString(tilesUser))
                .withExpiresAt(calendar.getTime());
        String token = builder.sign(Algorithm.HMAC256(KEY));
        return token;
    }
        /*
        * 验证
        * */
    public static DecodedJWT verify(String verifyToken) {
        DecodedJWT verify=null;
        try {
            verify=JWT.require(Algorithm.HMAC256(KEY)).build().verify(verifyToken);
        }catch (SignatureVerificationException e){
                e.printStackTrace();
            System.out.println("签名不一致");
        }catch (TokenExpiredException e){
            e.printStackTrace();
            System.out.println("令牌过期");
        }catch (AlgorithmMismatchException e){
            e.printStackTrace();
            System.out.println("签名算法不匹配");
        }catch (InvalidClaimException e){
            e.printStackTrace();
            System.out.println("payload不可用");
         }catch (Exception e){
            e.printStackTrace();
            System.out.println("效验失败");
        }
        return verify;
    }
    /*
    *
    * 验证通过再解析
    * 解析为Uuser对象
    * */
    public static TilesUser parse(DecodedJWT decodedJWT){
        Claim claim= decodedJWT.getClaim("UserInfo");
        if (claim!=null){
            String userString = claim.asString();
            TilesUser tilesUser = JSON.parseObject(userString, TilesUser.class);
            return tilesUser;
        }
        return null;
    }

    /*
    * 用户验证以后再解析
    * 解析出name
    * */
    public static String deJwtName(DecodedJWT decodedJWT){
        Claim claim = decodedJWT.getClaim("name");
        if (claim!=null){
            String name = claim.asString();
            return name;
        }
        return null;
    }

}
